<?

//require_once("class_PgMonSQL.php");
//require_once("classes/class_Hosts.php");
require_once("classes/class_WUHostCluster.php");

require_once("forms.php");
require_once("list.php");

function status_line() {
    echo "<div style=\"background-color: #DDD\">";
    if (isset($_SERVER['REQUEST_METHOD'])) {
	echo $_SERVER['REQUEST_METHOD']."</div>";
	print_r($_REQUEST);
    }
}

status_line();
if (isset($_GET['action'])) {
/************************ List Content ****************************/
    if ($_GET['action']=='list') {
	echo "<center>Configuration Page for PostgreSQL clusters</center></br>
	<div style=\"background-color: #DDD\">Cluster: </b></div><br>";
	if ($_GET['content']=='host_cluster') {
	    list_clusters_conf();
	} elseif ($_GET['content']=='database_name') {
	    list_cluster_databases($_GET['hc_id']);
	} elseif ($_GET['content']=='schema_name') {
	    list_database_schemas($_GET['dn_id']);
	}
/************************ Add Content *****************************/
    } elseif ($_GET['action']=='add') {
	if ($_GET['content']=='host_cluster') {
	    host_cluster_form();
	}
/************************ Edit Content ****************************/
    } elseif ($_GET['action']=='edit') {
	if ($_GET['content']=='host_cluster') {
	    host_cluster_form($_GET['hc_id']);
	} elseif ($_GET['content']=='database_name') {
	    edit_database_form($_GET['dn_id']);
	} elseif ($_GET['content']=='schema_name') {
	    edit_schema_form($_GET['sn_id']);
	}
/************************ Delete Cluster **************************/
    } elseif ($_GET['action']=='remove') {
	if ($_GET['content']=='host_cluster') {
	    remove_cluster_warn($_GET['hc_id']);
	}
    } elseif ($_GET['action']=='destroy') {
	if ($_GET['content']=='host_cluster') {
	    $hc=new HostCluster($_GET['hc_id']);
	    $hc->set_field('alive','f');
	    $hc->save();
	    list_clusters_conf();
	}
/************************ Discover Content ************************/
    } elseif ($_GET['action']=='discover') {
	if ($_GET['content']=='host_cluster') {
	    $hc=new HostCluster($_GET['hc_id']);
	    list_cluster_databases($_GET['hc_id'],$hc->discover());
	} elseif ($_GET['content']=='database_name') {
	    $sn=new DatabaseName($_GET['dn_id']);
	    list_database_schemas($_GET['dn_id'],$sn->discover());
	}
/**************************************************************/
/************************ Stat Content ************************/
    } elseif ($_GET['action']=='stat') {
	echo "<center>Statistic Page for PostgreSQL clusters</center></br>
	<div style=\"background-color: #DDD\">Cluster: </b></div><br>";
	if ($_GET['content']=='host_cluster') {
	    list_clusters_stat();
	} elseif ($_GET['content']=='database_name') {
	    list_databases_stat($_GET['hc_id']);
	}
    }
}






if (isset($_POST['add_cluster_submit'])) {
$hc=new HostCluster;

$hc->set_field('hostname',$_POST['add_cluster_hostname']);
$hc->set_field('ip_address',$_POST['add_cluster_ip_address']);
$hc->set_field('is_master',$_POST['add_cluster_is_master']);

$hc->set_field('alive','t');

if (isset($_POST['add_cluster_fqdn'])) {
$hc->set_field('fqdn',$_POST['add_cluster_fqdn']);
}
if (isset($_POST['add_cluster_spec_comments'])) {
    $hc->set_field('spec_comments',$_POST['add_cluster_spec_comments']);
}
/*****************/
$conn_param="{";
$conn_param.="hostaddr=".$_POST['add_cluster_ip_address'].",";

$conn_param.="dbname=".($_POST['add_cluster_conn_dbname'] ? $_POST['add_cluster_conn_dbname'] : 'postgres').",";
$conn_param.="port=".($_POST['add_cluster_conn_port'] ? $_POST['add_cluster_conn_port'] : '5432').",";
$conn_param.="user=".($_POST['add_cluster_conn_user'] ? $_POST['add_cluster_conn_user'] : 'postgres').",";
if ($_POST['add_cluster_conn_password']) {
$conn_param.="password=".$_POST['add_cluster_conn_password'].",";
}

$conn_param.="sslmode=".$_POST['add_cluster_conn_sslmode']."}";
$hc->set_field('conn_param',$conn_param);

$hc->save();

list_clusters_conf();
}

if (isset($_POST['edit_cluster_submit'])) {
    $hc=new HostCluster($_POST['edit_cluster_id']);
    $hc->update_cluster($_POST);
    list_host_clusters();
}

if (isset($_POST['edit_dn__submit'])) {
    $dn=new DatabaseName($_POST['edit_dn__dn_id']);
    $dn->update_database($_POST);
    list_cluster_databases($_POST['edit_dn__hc_id']);
}

if (isset($_POST['edit_sn__submit'])) {
    $sn=new SchemaName($_POST['edit_sn__sn_id']);
    $sn->update_schema($_POST);
    list_database_schemas($_POST['edit_sn__dn_id']);
}

?>
